package org.jeecg.modules.wlhy.hytaxpersonalincome.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.third.dongjiang.tax.system.dto.ApiBaseResDTO;
import org.jeecg.modules.wlhy.bill.entity.HyCarrierBill;
import org.jeecg.modules.wlhy.hytaxpersonalincome.entity.HyTaxPersonalincome;
import org.jeecg.modules.wlhy.hytaxpersonalincome.service.IHyTaxPersonalincomeService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.utils.DataMaskingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 个人所得税详细信息
 * @Author: duanlikao
 * @Date:   2021-03-19
 * @Version: V1.0
 */
@Api(tags="个人所得税详细信息")
@RestController
@RequestMapping("/hytaxpersonalincome/hyTaxPersonalincome")
@Slf4j
public class HyTaxPersonalincomeController extends JeecgController<HyTaxPersonalincome, IHyTaxPersonalincomeService> {
	@Autowired
	private IHyTaxPersonalincomeService hyTaxPersonalincomeService;

	@Resource
	private IHySettingService settingService;

	/**
	 * 分页列表查询
	 *
	 * @param hyTaxPersonalincome
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="个人所得税详细信息-分页列表查询", notes="个人所得税详细信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyTaxPersonalincome hyTaxPersonalincome,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyTaxPersonalincome> queryWrapper = QueryGenerator.initQueryWrapper(hyTaxPersonalincome, req.getParameterMap());
		Page<HyTaxPersonalincome> page = new Page<HyTaxPersonalincome>(pageNo, pageSize);
		IPage<HyTaxPersonalincome> pageList = hyTaxPersonalincomeService.page(page, queryWrapper);

		//如果开启系统脱敏
		String dataMaskingFlag = this.settingService.getSettingValueByTag("data_masking");
		if("1".equals(dataMaskingFlag)) {
			for (HyTaxPersonalincome dto : pageList.getRecords()) {
				dto.setSfzjhm(DataMaskingUtil.idcardMasking(dto.getSfzjhm()));
				dto.setLxdh(DataMaskingUtil.phoneMasking(dto.getLxdh()));
			}
		}

		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hyTaxPersonalincome
	 * @return
	 */
	@AutoLog(value = "个人所得税详细信息-添加")
	@ApiOperation(value="个人所得税详细信息-添加", notes="个人所得税详细信息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HyTaxPersonalincome hyTaxPersonalincome) {
		hyTaxPersonalincomeService.save(hyTaxPersonalincome);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hyTaxPersonalincome
	 * @return
	 */
	@AutoLog(value = "个人所得税详细信息-编辑")
	@ApiOperation(value="个人所得税详细信息-编辑", notes="个人所得税详细信息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyTaxPersonalincome hyTaxPersonalincome) {


		if(hyTaxPersonalincome.getSkssqq() != null && hyTaxPersonalincome.getSkssqz() != null ){
			if(hyTaxPersonalincome.getSkssqq().after(hyTaxPersonalincome.getSkssqz())){
				throw new JeecgBootException("税款所属期起日期应该在税款所属期止日期之前,请重新填写!");
			}
		}

		return hyTaxPersonalincomeService.updateById(hyTaxPersonalincome)?Result.OK("编辑成功!"):Result.error("编辑失败");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "个人所得税详细信息-通过id删除")
	@ApiOperation(value="个人所得税详细信息-通过id删除", notes="个人所得税详细信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hyTaxPersonalincomeService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "个人所得税详细信息-批量删除")
	@ApiOperation(value="个人所得税详细信息-批量删除", notes="个人所得税详细信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hyTaxPersonalincomeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="个人所得税详细信息-通过id查询", notes="个人所得税详细信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyTaxPersonalincome hyTaxPersonalincome = hyTaxPersonalincomeService.getById(id);
		if(hyTaxPersonalincome==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyTaxPersonalincome);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyTaxPersonalincome
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HyTaxPersonalincome hyTaxPersonalincome) {
        return super.exportXls(request, hyTaxPersonalincome, HyTaxPersonalincome.class, "个人所得税详细信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyTaxPersonalincome.class);
    }


	 @AutoLog(value = "个人所得税详细信息")
	 @ApiOperation(value="个人所得税详细信息", notes="个人所得税详细信息")
	 @PutMapping(value = "/taxupload")
	 public Result<?> taxupload(@RequestBody HyTaxPersonalincome HyTaxPersonalincome) {
		 ApiBaseResDTO reportResult = hyTaxPersonalincomeService.uploadTax(HyTaxPersonalincome);
		 Integer code =  StringUtils.isNotEmpty(reportResult.getCode())? Integer.valueOf(reportResult.getCode()) : 0;
		 if( code >= 100 && code < 200 ) {
			 return Result.OK("上报成功!");
		 }
		 return Result.error("上报失败!" + reportResult.getDesc()) ;
	 }


	 @AutoLog(value = "个人所得税详细信息-批量上报")
	 @ApiOperation(value="个人所得税详细信息-批量上报", notes="个人所得税详细信息-批量上报")
	 @PutMapping(value = "/taxuploadBatch")
	 public Result<?> taxuploadBatch(@RequestBody JSONObject jSONObject) {
		 String ids = jSONObject.getString("ids");

		 List <String> idStrings =  Arrays.asList(ids.split(","));
		 int i = 1;
		 for (String id: idStrings){
			 HyTaxPersonalincome hyTaxPersonalincome = new HyTaxPersonalincome();
			 hyTaxPersonalincome.setId(id);
			 ApiBaseResDTO reportResult = hyTaxPersonalincomeService.uploadTax(hyTaxPersonalincome);
			 Integer code =  StringUtils.isNotEmpty(reportResult.getCode())? Integer.valueOf(reportResult.getCode()) : 0;
			 if( code >= 100 && code < 200 ) {
				 i++;
			 }else {
				 return Result.error("上报失败!id:" + id +" desc:"+reportResult.getDesc() ) ;
			 }
		 }
		return Result.OK("上报成功!");
	 }
}
